package com.org.forex;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.DefaultHighLowDataset;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JSeparator;
import javax.swing.JPanel;
import java.awt.Choice;
import javax.swing.JLabel;
import javax.swing.JFormattedTextField;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JTextField;
import javax.swing.text.DefaultCaret;
import javax.swing.JSlider;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MainForm {
	private Price[] prices;
	private Price[] compare_prices;

	private String data_file = "C:\\e\\sonpt\\EURUSD60.csv";
	private String data_file_prefix = "C:\\e\\sonpt\\";
	private String sample_file_prefix = "C:\\FxPro - MetaTrader 4\\MQL4\\Files\\";
	private String sample_file = sample_file_prefix + "EURUSD60.csv";

	private JFrame frmFxPatternTool;
	private ChartPanel match_chartPanel;
	private ChartPanel sample_chartPanel;
	private ChartPanel next_chartPanel;
	private ChartPanel back_stab_panel;
	private Choice choice_match_num;
	private JFormattedTextField candleNum;
	private JCheckBox chckbxIsContinuos;
	private JFormattedTextField nextcandleNum;
	private Choice timeChoice;
	private volatile boolean lock = false;
	private Choice symbolChoice;
	private Thread update_thread;
	private JTextField txtStartYear;
	private JTextField txtEndYear;
	private ChartPanel next_sample_panel;
	private JTextField txtOffset;
	private JPanel stdoutPanel;
	private JTextArea textArea;
	private JTextField txtDiff;
	private JSlider sliderCandlenum;
	private JSlider sliderOffset;
	private ChartPanel match_next_panel;
	private JLabel lbnOffset;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainForm window = new MainForm();
					window.frmFxPatternTool.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainForm() {
		initialize();
		// monitor the sample price;
		update_thread = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				updatePrice();
			}
		});
		update_thread.start();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmFxPatternTool = new JFrame();
		frmFxPatternTool.setTitle("FX Pattern Tool");
		frmFxPatternTool.setBounds(100, 100, 1625, 647);
		frmFxPatternTool.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmFxPatternTool.getContentPane().setLayout(null);

		JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 1214, 21);
		frmFxPatternTool.getContentPane().add(menuBar);

		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);

		JMenuItem mntmLoadSampleFile = new JMenuItem("Load Sample File");
		mntmLoadSampleFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				loadSamplePrice();
			}
		});
		mnFile.add(mntmLoadSampleFile);

		JMenuItem mntmLoadDataFile = new JMenuItem("Load Data File");
		mntmLoadDataFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loadPrice();
			}
		});
		mnFile.add(mntmLoadDataFile);

		JSeparator separator = new JSeparator();
		mnFile.add(separator);

		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frmFxPatternTool.dispose();
			}
		});
		mnFile.add(mntmExit);

		JMenu mnChart = new JMenu("Chart");
		menuBar.add(mnChart);

		JMenuItem mntmDraw = new JMenuItem("Draw");
		mntmDraw.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				drawChart();
			}
		});
		mnChart.add(mntmDraw);

		JMenuItem mntmClear = new JMenuItem("Clear");
		mntmClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearChart();
			}
		});
		mnChart.add(mntmClear);

		JMenu mnConfig = new JMenu("Config");
		menuBar.add(mnConfig);

		JMenuItem mntmConfigSampleFile = new JMenuItem("Config Sample File");
		mntmConfigSampleFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Create a file chooser
				final JFileChooser fc = new JFileChooser();
				fc.setSelectedFile(new File(sample_file));
				// In response to a button click:
				fc.showOpenDialog(frmFxPatternTool);
				File file = fc.getSelectedFile();
				sample_file = file.getAbsolutePath();
				System.out.println(file.getAbsolutePath());
			}
		});
		mnConfig.add(mntmConfigSampleFile);

		JMenuItem mntmConfigDataFile = new JMenuItem("Config Data File");
		mntmConfigDataFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Create a file chooser
				final JFileChooser fc = new JFileChooser();
				fc.setSelectedFile(new File(data_file));
				// In response to a button click:
				fc.showOpenDialog(frmFxPatternTool);
				File file = fc.getSelectedFile();
				data_file = file.getAbsolutePath();
				System.out.println(file.getAbsolutePath());
			}
		});
		mnConfig.add(mntmConfigDataFile);

		sample_chartPanel = new ChartPanel(null);
		sample_chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		sample_chartPanel.setBounds(10, 314, 397, 271);
		frmFxPatternTool.getContentPane().add(sample_chartPanel);

		match_chartPanel = new ChartPanel(null);
		match_chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		match_chartPanel.setBounds(10, 32, 397, 271);
		frmFxPatternTool.getContentPane().add(match_chartPanel);

		next_chartPanel = new ChartPanel(null);
		next_chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		next_chartPanel.setBounds(417, 32, 383, 271);
		frmFxPatternTool.getContentPane().add(next_chartPanel);

		next_sample_panel = new ChartPanel(null);
		next_sample_panel.setBounds(417, 314, 383, 271);
		next_sample_panel.setPreferredSize(new java.awt.Dimension(500, 270));
		frmFxPatternTool.getContentPane().add(next_sample_panel);

		JPanel panel = new JPanel();
		panel.setBounds(1216, 32, 383, 344);
		frmFxPatternTool.getContentPane().add(panel);
		panel.setLayout(null);

		choice_match_num = new Choice();
		choice_match_num.setBounds(21, 24, 73, 20);
		choice_match_num.add("1");
		choice_match_num.add("2");
		choice_match_num.add("3");
		choice_match_num.add("4");
		choice_match_num.add("5");
		choice_match_num.add("6");
		choice_match_num.add("7");
		choice_match_num.add("8");
		choice_match_num.add("9");
		choice_match_num.add("10");
		panel.add(choice_match_num);

		JLabel lblNewLabel = new JLabel("Match num");
		lblNewLabel.setBounds(101, 24, 67, 14);
		panel.add(lblNewLabel);

		JLabel lblCandleNum = new JLabel("Candle num");
		lblCandleNum.setBounds(101, 63, 67, 14);
		panel.add(lblCandleNum);

		candleNum = new JFormattedTextField();
		candleNum.setText("48");
		candleNum.setBounds(21, 60, 73, 17);

		panel.add(candleNum);

		JButton btnApply = new JButton("Apply");
		btnApply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (!update_thread.isAlive()) {
					update_thread = new Thread(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							updatePrice();
						}
					});
					update_thread.start();
				}
			}
		});
		btnApply.setBounds(270, 214, 89, 23);
		panel.add(btnApply);

		chckbxIsContinuos = new JCheckBox("Is Continuos");
		chckbxIsContinuos.setBounds(169, 24, 97, 23);
		panel.add(chckbxIsContinuos);

		nextcandleNum = new JFormattedTextField();
		nextcandleNum.setText("48");
		nextcandleNum.setBounds(169, 60, 83, 17);
		panel.add(nextcandleNum);

		JLabel lblNextCandleNum = new JLabel("Next Candle num");
		lblNextCandleNum.setBounds(262, 63, 97, 14);
		panel.add(lblNextCandleNum);

		timeChoice = new Choice();
		timeChoice.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				System.out.println("Time frame changed");
				updateAllData();
			}
		});
		timeChoice.setBounds(169, 94, 83, 20);
		timeChoice.add("1H");
		timeChoice.add("4H");
		timeChoice.add("1D");
		timeChoice.add("1W");
		timeChoice.add("1M");
		panel.add(timeChoice);

		JLabel lblTimeFrame = new JLabel("Time Frame");
		lblTimeFrame.setBounds(262, 100, 83, 14);
		panel.add(lblTimeFrame);

		symbolChoice = new Choice();
		symbolChoice.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				System.out.println("Symbol changed");
				updateAllData();
			}
		});
		symbolChoice.setBounds(169, 131, 83, 20);
		symbolChoice.add("EURUSD");
		symbolChoice.add("GOLD");
		symbolChoice.add("GBPUSD");
		symbolChoice.add("USDJPY");
		panel.add(symbolChoice);

		JLabel lblSymbol = new JLabel("Symbol");
		lblSymbol.setBounds(260, 131, 46, 14);
		panel.add(lblSymbol);

		txtStartYear = new JTextField();
		txtStartYear.setText("1999");
		txtStartYear.setBounds(21, 215, 86, 20);
		panel.add(txtStartYear);
		txtStartYear.setColumns(10);

		txtEndYear = new JTextField();
		txtEndYear.setText("2012");
		txtEndYear.setColumns(10);
		txtEndYear.setBounds(169, 215, 86, 20);
		panel.add(txtEndYear);

		JLabel lblEndYear = new JLabel("End Year");
		lblEndYear.setBounds(169, 246, 83, 14);
		panel.add(lblEndYear);

		JLabel lblStartYear = new JLabel("Start Year");
		lblStartYear.setBounds(21, 246, 89, 14);
		panel.add(lblStartYear);

		JLabel lblNewLabel_1 = new JLabel("Offset");
		lblNewLabel_1.setBounds(101, 100, 46, 14);
		panel.add(lblNewLabel_1);

		txtOffset = new JTextField();
		txtOffset.setText("100");
		txtOffset.setBounds(21, 94, 73, 20);
		panel.add(txtOffset);
		txtOffset.setColumns(10);

		txtDiff = new JTextField();
		txtDiff.setText("50");
		txtDiff.setBounds(21, 131, 73, 20);
		panel.add(txtDiff);
		txtDiff.setColumns(10);

		JLabel lblDiff = new JLabel("Diff");
		lblDiff.setBounds(101, 137, 46, 14);
		panel.add(lblDiff);

		sliderCandlenum = new JSlider();
		sliderCandlenum.setMinorTickSpacing(1);
		sliderCandlenum.setMajorTickSpacing(40);
		sliderCandlenum.setSnapToTicks(true);
		sliderCandlenum.setPaintLabels(true);
		sliderCandlenum.setPaintTicks(true);
		sliderCandlenum.setMaximum(200);
		sliderCandlenum.setBounds(21, 162, 231, 42);
		panel.add(sliderCandlenum);

		JLabel lblCandleNum_1 = new JLabel("Candle num");
		lblCandleNum_1.setBounds(260, 171, 85, 14);
		panel.add(lblCandleNum_1);

		JLabel lblOffset = new JLabel("Offset");
		lblOffset.setBounds(333, 291, 46, 14);
		panel.add(lblOffset);

		lbnOffset = new JLabel("");
		lbnOffset.setBounds(333, 264, 46, 14);
		panel.add(lbnOffset);

		sliderOffset = new JSlider();
		sliderOffset.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				lbnOffset.setText(sliderOffset.getValue() + "");
			}
		});
		sliderOffset.setMinorTickSpacing(1);
		sliderOffset.setMajorTickSpacing(100);
		sliderOffset.setValue(100);
		sliderOffset.setSnapToTicks(true);
		sliderOffset.setPaintTicks(true);
		sliderOffset.setPaintLabels(true);
		sliderOffset.setMaximum(1000);
		sliderOffset.setBounds(47, 264, 231, 42);
		panel.add(sliderOffset);

		JButton button = new JButton("-");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				sliderOffset.setValue(sliderOffset.getValue() + 1);
			}
		});
		button.setBounds(0, 271, 37, 23);
		panel.add(button);

		JButton button_1 = new JButton("+");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				sliderOffset.setValue(sliderOffset.getValue() + 1);
			}
		});
		button_1.setBounds(281, 271, 46, 23);
		panel.add(button_1);

		textArea = new JTextArea(25, 80);
		textArea.setEditable(false);

		stdoutPanel = new JPanel();
		stdoutPanel.setBounds(1216, 387, 383, 211);
		stdoutPanel.setLayout(new BorderLayout());
		frmFxPatternTool.getContentPane().add(stdoutPanel);
		stdoutPanel.add(new JScrollPane(textArea,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS), BorderLayout.CENTER);
		JTextAreaOutputStream out = new JTextAreaOutputStream(textArea);

		back_stab_panel = new ChartPanel(null);
		back_stab_panel.setBounds(809, 314, 397, 271);
		frmFxPatternTool.getContentPane().add(back_stab_panel);

		match_next_panel = new ChartPanel(null);
		match_next_panel.setBounds(810, 32, 397, 271);
		frmFxPatternTool.getContentPane().add(match_next_panel);
		System.setOut(new PrintStream(out));
		DefaultCaret caret = (DefaultCaret) textArea.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

		loadPrice();
		// loadSamplePrice();
		drawChart();
	}

	private void updateAllData() {
		System.out.println("changed");
		lock = true;
		switch (timeChoice.getSelectedIndex()) {
		case 0:
			System.out.println("1h");
			data_file = data_file_prefix + symbolChoice.getSelectedItem()
					+ "60.csv";
			sample_file = sample_file_prefix + symbolChoice.getSelectedItem() + "60.csv";
			break;
		case 1:
			System.out.println("4h");
			data_file = data_file_prefix + symbolChoice.getSelectedItem()
					+ "240.csv";
			sample_file = sample_file_prefix + symbolChoice.getSelectedItem()
					+ "240.csv";
			break;
		case 2:
			System.out.println("1d");
			data_file = data_file_prefix + symbolChoice.getSelectedItem()
					+ "1440.csv";
			sample_file = sample_file_prefix + symbolChoice.getSelectedItem()
					+ "1440.csv";
			break;
		case 3:
			System.out.println("1w");
			data_file = data_file_prefix + symbolChoice.getSelectedItem()
					+ "10080.csv";
			sample_file = sample_file_prefix + symbolChoice.getSelectedItem()
					+ "10080.csv";
			break;
		case 4:
			System.out.println("1m");
			data_file = data_file_prefix + symbolChoice.getSelectedItem()
					+ "43200.csv";
			sample_file = sample_file_prefix + symbolChoice.getSelectedItem()
					+ "43200.csv";
			break;
		default:
			break;
		}
		loadPrice();
		loadSamplePrice();
		lock = false;
	}

	private void loadPrice() {
		try {
			prices = DataUtils.loadCSVFile(data_file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void updatePrice() {
		while (true) {
			try {
				// if lock then do not update price
				if (lock)
					return;
				// loadSamplePrice();
				drawChart();
				Thread.sleep(1000);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void loadSamplePrice() {
		try {
			compare_prices = DataUtils.loadCSVFile(sample_file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void clearChart() {
		sample_chartPanel.setChart(null);
		match_chartPanel.setChart(null);
		next_chartPanel.setChart(null);
	}

	private int min(int x1, int x2) {
		return x1 < x2 ? x1 : x2;
	}

	private void drawChart() {
		// long start_time = System.currentTimeMillis();
		int size = sliderCandlenum.getValue();
		int offset = sliderOffset.getValue();
		int next_size = Integer.parseInt(nextcandleNum.getText());
		compare_prices = new Price[size];
		// change compare price to offset
		System.arraycopy(prices, prices.length - size - offset, compare_prices,
				0, size);

		int start = compare_prices.length - size;

		int[] index = Price.getAllDiff(prices, compare_prices, start, size,
				chckbxIsContinuos.isSelected(),
				Integer.parseInt(txtStartYear.getText()),
				Integer.parseInt(txtEndYear.getText()),
				Integer.parseInt(txtDiff.getText()));
		// System.out.println(prices[index[choice.getSelectedIndex()]]);
		// print out information
		System.out.println("index : "
				+ index[choice_match_num.getSelectedIndex()]);
		double diff_current = Price.diffOpen(prices,
				index[choice_match_num.getSelectedIndex()], compare_prices, 0,
				size)
				+ Price.diffClose(prices,
						index[choice_match_num.getSelectedIndex()],
						compare_prices, 0, size);
		System.out.println("mau thu " + choice_match_num.getSelectedIndex());
		System.out.println("index : "
				+ index[choice_match_num.getSelectedIndex()]);
		System.out.println("do lech hien tai " + diff_current);
		try {
			double diff_next = Price.diffOpen(prices,
					index[choice_match_num.getSelectedIndex()] + size, prices,
					prices.length - offset, size)
					+ Price.diffClose(prices,
							index[choice_match_num.getSelectedIndex()] + size,
							prices, prices.length - offset, size);
			System.out.println("do lech tiep theo " + diff_next);
		} catch (ArrayIndexOutOfBoundsException e) {
			// TODO: handle exception
			System.out.println("next diff not available");
		}

		boolean scaleday = true;
		if (timeChoice.getSelectedIndex() > 1)
			scaleday = false;
		// Draw sample chart
		final DefaultHighLowDataset sample_dataset = CandlestickDemo
				.createHighLowDataset(compare_prices, start, start + size,
						scaleday);

		final JFreeChart sample_chart = CandlestickDemo
				.createChart(sample_dataset);
		sample_chart.setTitle("Sample chart");
		sample_chart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);
		sample_chartPanel.setChart(sample_chart);
		sample_chartPanel.repaint();
		// Draw next sample chart

		try {
			final DefaultHighLowDataset next_sample_dataset = CandlestickDemo
					.createHighLowDataset(prices, prices.length - offset,
							prices.length - offset + next_size, scaleday);

			final JFreeChart next_sample_chart = CandlestickDemo
					.createChart(next_sample_dataset);
			next_sample_chart.setTitle("Next Sample chart");
			next_sample_chart.getXYPlot().setOrientation(
					PlotOrientation.VERTICAL);
			next_sample_panel.setChart(next_sample_chart);
			next_sample_panel.repaint();
		} catch (ArrayIndexOutOfBoundsException e) {
			// TODO: handle exception
			System.out.println("next sample chart not available");
			next_sample_panel.setChart(null);
		}

		// Draw match chart
		final DefaultHighLowDataset match_dataset = CandlestickDemo
				.createHighLowDataset(prices,
						index[choice_match_num.getSelectedIndex()],
						index[choice_match_num.getSelectedIndex()] + size,
						scaleday);
		final JFreeChart match_chart = CandlestickDemo
				.createChart(match_dataset);
		match_chart.setTitle("Match chart");
		match_chart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);
		match_chartPanel.setChart(match_chart);
		match_chartPanel.repaint();

		// Draw next chart
		if (index[choice_match_num.getSelectedIndex()] + size >= prices.length - 1) {
			System.out.println("next match chart not available");
		} else {
			final DefaultHighLowDataset next_dataset = CandlestickDemo
					.createHighLowDataset(
							prices,
							index[choice_match_num.getSelectedIndex()] + size,
							min(index[choice_match_num.getSelectedIndex()]
									+ size + next_size, prices.length - 1),
							scaleday);
			final JFreeChart next_chart = CandlestickDemo
					.createChart(next_dataset);
			next_chart.setTitle("Next chart");
			next_chart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);

			DefaultCaret caret = (DefaultCaret) textArea.getCaret();
			caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

			next_chartPanel.setChart(next_chart);
			next_chartPanel.repaint();

			// Draw back stab panel
			Price[] back_stab_compare_prices = new Price[size];
			System.arraycopy(prices, index[choice_match_num.getSelectedIndex()]
					+ size, back_stab_compare_prices, 0, size);
			int[] back_stab_index = Price.getAllDiff(prices,
					back_stab_compare_prices, start, size,
					chckbxIsContinuos.isSelected(),
					Integer.parseInt(txtStartYear.getText()),
					Integer.parseInt(txtEndYear.getText()),
					Integer.parseInt(txtDiff.getText()));
			final DefaultHighLowDataset back_stab_dataset = CandlestickDemo
					.createHighLowDataset(
							prices,
							back_stab_index[choice_match_num.getSelectedIndex()]
									- size,
							min(back_stab_index[choice_match_num
									.getSelectedIndex()] - size + next_size,
									prices.length - 1), scaleday);
			final JFreeChart back_stab_chart = CandlestickDemo
					.createChart(back_stab_dataset);
			back_stab_chart.setTitle("Back stab chart");
			back_stab_chart.getXYPlot()
					.setOrientation(PlotOrientation.VERTICAL);
			back_stab_panel.setChart(back_stab_chart);
			back_stab_panel.repaint();

			// Draw next match panel

			final DefaultHighLowDataset match_next_dataset = CandlestickDemo
					.createHighLowDataset(
							prices,
							back_stab_index[choice_match_num.getSelectedIndex()],
							min(back_stab_index[choice_match_num
									.getSelectedIndex()] + next_size,
									prices.length - 1), scaleday);
			final JFreeChart match_next_chart = CandlestickDemo
					.createChart(match_next_dataset);
			match_next_chart.setTitle("Match next chart");
			match_next_chart.getXYPlot().setOrientation(
					PlotOrientation.VERTICAL);
			match_next_panel.setChart(match_next_chart);
			match_next_panel.repaint();

		}

		// frmFxPatternTool.getContentPane().validate();
		// frmFxPatternTool.getContentPane().repaint();

		/*
		 * long end = System.currentTimeMillis();
		 * System.out.println("Execution time was " + (end - start_time) +
		 * " ms."); Runtime runtime = Runtime.getRuntime();
		 * 
		 * NumberFormat format = NumberFormat.getInstance();
		 * 
		 * StringBuilder sb = new StringBuilder(); long maxMemory =
		 * runtime.maxMemory(); long allocatedMemory = runtime.totalMemory();
		 * long freeMemory = runtime.freeMemory();
		 * 
		 * sb.append("free memory: " + format.format(freeMemory / 1024) +
		 * "<br/>"); sb.append("allocated memory: " +
		 * format.format(allocatedMemory / 1024) + "<br/>");
		 * sb.append("max memory: " + format.format(maxMemory / 1024) +
		 * "<br/>"); sb.append("total free memory: " + format.format((freeMemory
		 * + (maxMemory - allocatedMemory)) / 1024) + "<br/>");
		 * System.out.println(sb.toString());
		 */
	}
}
